//
//  NoticeVC.m
//  Xfdsh
//
//  Created by aa on 01/03/2018.
//  Copyright © 2018 金刚葫芦娃. All rights reserved.
//
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

#import "NoticeVC.h"
#import "NoticeTableVC.h"
#import "SystemInforObject.h"
@interface NoticeVC ()<UITableViewDelegate,UITableViewDataSource>
{
 
}
@property (nonatomic, strong) UITableView * tableVeiw;
@property (strong, nonatomic) NSIndexPath* editingIndexPath;  //当前左滑cell的index，在代理方法中设置
@property (nonatomic, strong) NSMutableArray * statusArray;
@property (nonatomic, strong) NSMutableArray * dataArray;
@end

@implementation NoticeVC
- (UITableView *)tableVeiw{
    if (!_tableVeiw) {
        _tableVeiw = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableVeiw.backgroundColor = [UIColor whiteColor];
        _tableVeiw.delegate = self;
        _tableVeiw.dataSource = self;
        _tableVeiw.showsVerticalScrollIndicator = NO;
        _tableVeiw.showsHorizontalScrollIndicator = NO;
        _tableVeiw.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableVeiw registerNib:[UINib nibWithNibName:@"NoticeTableVC" bundle:nil] forCellReuseIdentifier:@"Cell"];
    }
    return _tableVeiw;
}
- (void)viewDidAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"通知";
    _statusArray = [NSMutableArray arrayWithCapacity:20];
    _dataArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<20; i++) {
        [_statusArray addObject:@0];
    }
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    button.bounds = CGRectMake(0, 0, 30, 30);
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self createView];
    [self getSysstemMessage];
}
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createView{
    if (@available(iOS 11.0, *)) {
        self.tableVeiw.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableVeiw];
    [_tableVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0).offset(0);
        make.top.equalTo(@0).offset([[UIApplication sharedApplication] statusBarFrame].size.height+44);
        make.bottom.equalTo(@0);
    }];
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * topView = [[UIView alloc]init];
//    topView.frame = CGRectMake(0, 0, MAINSCREEN.size.width, 0);
//    topView.backgroundColor = [UIColor clearColor];
//    UILabel * leftLable = [[UILabel alloc]init];
//    leftLable.textColor = [UIColor grayColor];
//    leftLable.font = [UIFont systemFontOfSize:17];
//    leftLable.text = @"全部标记为已读";
//    [topView addSubview:leftLable];
//    [leftLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@20);
//        make.centerY.equalTo(topView.mas_centerY);
//        make.width.equalTo(@160);
//        make.height.equalTo(@50);
//    }];
//
//    UIButton * rightImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [rightImageBtn addTarget:self action:@selector(rightOptionClick:) forControlEvents:UIControlEventTouchUpInside];
//    [rightImageBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    rightImageBtn.backgroundColor = [UIColor lightGrayColor];
//    [topView addSubview:rightImageBtn];
//    [rightImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(@0).offset(-20);
//        make.centerY.equalTo(leftLable.mas_centerY);
//        make.height.equalTo(@30);
//        make.width.equalTo(@30);
//    }];
    return topView;
}
- (void)rightOptionClick:(UIButton *)sender{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //改变状态为已读
//    _statusArray[indexPath.row] = @1;

//    //进入相应的
//    SystemInforObject * object = [[SystemInforObject alloc]init];
//    object = _dataArray[indexPath.row];
//    //   改变状态已读
//    [_tableVeiw reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:0];

}
#pragma mark 取消选中行
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeTableVC * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[NoticeTableVC alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.topLabel.text = @"标题";
    cell.detailLabel.text = @"内容";
    cell.backgroundColor = [UIColor whiteColor];
    cell.timeLabel.text = @"时间";
    cell.isReadLable.text = @"已读";
    cell.isReadLable.backgroundColor = [UIColor lightGrayColor];
    return cell;
}
#pragma mark - 返回编辑模式，默认为删除模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
    if (editingStyle == UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
        SystemInforObject * object = [[SystemInforObject alloc]init];
        object = _dataArray[indexPath.row];
    }else {
        
    }
}
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Tapped accessory button");
}
#pragma mark - 左滑多个菜单
//- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
//{
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if (self.editingIndexPath)
    {
        [self configSwipeButtons];
    }
}
- (void)configSwipeButtons
{
    // 获取选项按钮的reference
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")){
        for (UIView *subview in _tableVeiw.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
            {
                subview.backgroundColor = [UIColor whiteColor];
                // 和iOS 10的按钮顺序相反
                UIButton *deleteButton = subview.subviews[0];
                deleteButton.frame = CGRectMake(0, 10, 70, 100);
                deleteButton.backgroundColor = [UIColor whiteColor];
                [deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];
                [self configDeleteButton:deleteButton];
                [self centerImageAndTextOnButton:deleteButton];
            }
        }
    }else{
        // iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
        NoticeTableVC *tableCell = [_tableVeiw cellForRowAtIndexPath:self.editingIndexPath];
        for (UIView *subview in tableCell.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])
            {
                subview.backgroundColor = [UIColor whiteColor];
                UIButton *deleteButton = subview.subviews[0];
                
                [self configDeleteButton:deleteButton];
                deleteButton.frame = CGRectMake(0, 0, 60, 110);
                deleteButton.backgroundColor = [UIColor whiteColor];
                [deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];
                [self configDeleteButton:deleteButton];
                [self centerImageAndTextOnButton:deleteButton];
            }
        }
    }
}
- (void)centerImageAndTextOnButton:(UIButton*)button
{
    if (SYSTEM_VERSION_LESS_THAN(@"11.0"))
    {
        CGRect btnFrame = button.frame;
        btnFrame.origin.y = 5;
        button.frame = btnFrame;
    }else{
        
    }
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.editingIndexPath = indexPath;
    [self.view setNeedsLayout];   // 触发-(void)viewDidLayoutSubviews
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.editingIndexPath = nil;
}
- (void)configDeleteButton:(UIButton*)deleteButton
{
    if (deleteButton)
    {
   //点击删除按钮触发的方法
    }
}

- (void)configReadButton:(UIButton*)readButton
{
    if (readButton)
    {

    }
}
#pragma mark - 获得系统消息
- (void)getSysstemMessage{

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
